for knd=1:length(ROIs)
    r_act_mat(:,knd)=ROIs(knd).activity;
end
sig_act=[];
parfor knd=1:length(ROIs)
    tmp=find_active_ind(r_act_mat(:,knd),4);
    sig_act(:,knd)=tmp;
end

% low_freq=0.5;
% high_freq=3;
% 
% idx=[1:size(sig_act,1)];
% sound_mat=zeros(size(sig_act));
% 
% for ind=1:size(sig_act,2)
%     curr_freq=low_freq+(high_freq-low_freq)/size(sig_act,2)*ind;
%     sound_mat(:,ind)=sin(idx*curr_freq);
% end
% 
% so=sum(sound_mat.*sig_act,2);


fs_act=482;

so=zeros(1,round(size(sig_act,1)/fs_act*10000));
for ind=1:size(sig_act,2)%[68 30 36 69 27 71 70 64 32 38 22 18 15 21 23 35 44]%
    tmp=sig_act(:,ind);
    tmp(1)=0;
    tmp(end)=0;
    onsets=find(diff(tmp)==1);
    offsets=find(diff(tmp)==-1);
    [row_ind,col_ind]=ind2sub(size(template),min(ROIs(ind).indices));

    for knd=1:length(onsets)
        %keysnip=key2note(length(ROIs(ind).indices)*min(1,(offsets(knd)-onsets(knd))/20),round((1.5*row_ind - col_ind +622)/12),(offsets(knd)-onsets(knd))/fs_act);
        keysnip=key2note(length(ROIs(ind).indices)*min(1,(offsets(knd)-onsets(knd))/20),ind+10,(offsets(knd)-onsets(knd))/fs_act);
        keysnip=keysnip(1:end-1);
        so(round(onsets(knd)*10000/fs_act):round(onsets(knd)*10000/fs_act)+length(keysnip)-1)=so(round(onsets(knd)*10000/fs_act):round(onsets(knd)*10000/fs_act)+length(keysnip)-1)+keysnip;
    end
end

%wavwrite(so/max(so),10000,'cort_songs_rand.wav')


